/**	 
 * @Description 
 * @Path		com.bbx.api.sdk.net.task.BaseAsyncTask.java 
 * @Author		年向   
 * @Date		2015年7月15日 下午5:22:26 
 * @Company	 	蓝海（福建）信息技术有限公司	 
 */
package com.bbx.taxi.client.Task;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
import android.text.TextUtils;

import com.bbx.androidapi.util.SystemUtil;
import com.bbx.api.sdk.net.base.BaseNetwork;
import com.bbx.api.sdk.net.base.HttpComm;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.Service.LoginService;
import com.bbx.taxi.client.Service.TokenService;
import com.bbx.taxi.client.Util.IsServiceRunning;
import com.bbx.taxi.client.Util.NetworkDetector;
import com.bbx.taxi.client.Util.ServiceRunning;
import com.bbx.taxi.official.R;
import com.example.loadview.LoadingDialog;

public abstract class BaseAsyncTask extends AsyncTask<Object, Object, Object> {

	public int DELAY_TIME = 0;
	public static int OUTTIME_TOKEN = 200;

	public abstract void onSuccess(Object object);

	public abstract void onFailed(int status, String msg);

	/** 用于区分不同的请求类型 */
	public int type;
	/** 是否现在加载框 */
	private boolean isShow = true;
	protected Context context;
	private LoadingDialog dialog;
	private String msg;
	private boolean isReturnString = false;

	public boolean isLoading = false;
	public boolean isLoadCancel = true;
	public Handler handler = new Handler(MyApplication.getInstance().getApplicationContext().getMainLooper());

	private void initDialog() {
		dialog = new LoadingDialog(context, isLoadCancel);
		if (msg == null || TextUtils.isEmpty(msg)) {
			msg = context.getString(R.string.loading);

		}
	}

	private void showDialog() {
		try {
			handler.postDelayed(new Runnable() {
				
				@Override
				public void run() {
					if (isLoading && dialog != null && !dialog.isShowing() && isShow) {
						dialog.show();
					}
				}
			}, 500);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void disDialog() {
		try {
			if (dialog != null) {
				dialog.onDismiss();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public BaseAsyncTask(Context context, String msg, boolean isShow) {
		this.context = context;
		this.msg = msg;
		this.isShow = isShow;
		initDialog();
		 ServiceRunning.IsServiceRunning(context, true);
	}

	public BaseAsyncTask(Context context, boolean isShow) {
		this.context = context;
		this.isShow = isShow;
		initDialog();
		 ServiceRunning.IsServiceRunning(context, true);
	}

	public BaseAsyncTask(Context context) {
		this.context = context;
		initDialog();
		 ServiceRunning.IsServiceRunning(context, true);
	}

	/**
	 * isDetecte==1检测，其余不检测
	 */
	public BaseAsyncTask(Context context, int isDetecte) {
		this.context = context;
		initDialog();
		 ServiceRunning.IsServiceRunning(context, isDetecte == 1 ? true : false);
	}

	/**
	 * isDetecte==1检测，其余不检测
	 */
	public BaseAsyncTask(Context context, boolean isShow, int isDetecte) {
		this.context = context;
		this.isShow = isShow;
		initDialog();
		 ServiceRunning.IsServiceRunning(context, isDetecte == 1 ? true : false);
	}

	@Override
	protected void onPreExecute() {
		if (NetworkDetector.detect(context)) {
			super.onPreExecute();
			isLoading = true;
			if (isShow)
				handler.postDelayed(showDialog, DELAY_TIME);
		} else {
			disDialog();
			onFailed(0, "default err msg");
		}

	}

	public Runnable showDialog = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			showDialog();
		}
	};

	@Override
	protected void onPostExecute(Object result) {
		if (NetworkDetector.detect(context)) {
			// TODO Auto-generated method stub
			isLoading = false;
			if (result instanceof BaseNetwork) {
				BaseNetwork baseNetwork = (BaseNetwork) result;
				if (baseNetwork != null && baseNetwork.getSuccess()) {
					if (isReturnString) {
						onSuccess(baseNetwork.getString());
					} else {
						onSuccess(baseNetwork.getData());
					}
				} else {
					int code = baseNetwork.getCode();
					// token过期
					if (code == -10105 || code == -10108) {
						// 判断Token
						context.startService(new Intent(context,
								TokenService.class));
					}
					onFailed(code, baseNetwork.getString());
				}
			} else {
				onFailed(HttpComm.TIMEOUT_CODE, "default err msg");
			}
			disDialog();
			super.onPostExecute(result);
		} else {
			onFailed(HttpComm.TIMEOUT_CODE, "no network");
		}
	}

	/**
	 * 用于区分不同的请求类型
	 */
	protected int getType() {
		return type;
	}

	protected void setType(int type) {
		this.type = type;
	}

	/**
	 * 返回类型有String类型
	 */
	public void setIsReturnString(boolean isReturnString) {
		this.isReturnString = isReturnString;
	}

	/**
	 * 设置加载框是否可取消，默认可以
	 */
	public void setLoadCancel(boolean isLoadCancel) {
		this.isLoadCancel = isLoadCancel;
	}

	/** @Description 关闭act */
	public void finishAct() {
		if (isAct()) {
			Activity activity = (Activity) context;
			if (!activity.isFinishing()) {
				activity.finish();

			}
		}

	}

	/** @Description 是否为activity */
	public boolean isAct() {
		if (context instanceof Activity) {
			return true;
		}
		return false;
	}
}
